package com.hex2int;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MAC2INT {

	public static void main(String[] args) {
		String mac = "E7:F2:7A:01:00:4B:12:00";
		String macNo = "E7F27A01004B1200";
		System.out.println(mac);
		byte[] macBytes = getZigbeeMacBytes(mac);
		System.out.println(Arrays.toString(macBytes));
		String macString = getZigbeeMacString(macBytes);
		System.out.println(macString);
		
		
		byte[] create_afterpanid_network_ = new byte[]{0x01,10,9,
				0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
				0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
				0x03,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
				0x04,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
				0x22,0x58,(byte) 0xB4,0x01,0x00,0x4B,0x12,0x00,(byte) 0xFF,(byte) 0xFF,
				(byte) 0xE7,(byte) 0xF2,0x7A,0x01,0x00,0x4B,0x12,0x00,(byte) 0xFF,(byte) 0XFF,
				(byte) 0xEB,(byte) 0xEF,(byte) 0xB4,0x04,0x00,0x4B,0x12,0x00,(byte) 0xFF,(byte) 0xFF,
				(byte) 0x8C,(byte) 0xCF,0x30,0x02,0x00,0x4B,0x12,0x00,(byte) 0xFF,(byte) 0xFF,
				(byte) 0xD2,(byte) 0xF1,0x7A,0x01,0x00,0x4B,0x12,0x00,(byte) 0xFF,(byte) 0xFF,
				0x6B,(byte) 0xF1,0x7A,0x01,0x00,0x4B,0x12,0x00,(byte) 0xFF,(byte) 0xFF,
		};
		System.out.print(Arrays.toString(create_afterpanid_network_));
		

		List<String> list = new ArrayList<String>();
		
		list.add(getZigbeeMacString(new byte[]{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}));
		list.add(getZigbeeMacString(new byte[]{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}));
		list.add(getZigbeeMacString(new byte[]{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}));
		list.add(getZigbeeMacString(new byte[]{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}));
		list.add(getZigbeeMacString(new byte[]{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}));
		
		list.add(getZigbeeMacString(new byte[]{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}));
		list.add(getZigbeeMacString(new byte[]{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}));
		list.add(getZigbeeMacString(new byte[]{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}));
		list.add(getZigbeeMacString(new byte[]{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}));
		list.add(getZigbeeMacString(new byte[]{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}));
//		list.add(getZigbeeMacString(new byte[]{0x22,0x58,(byte) 0xB4,0x01,0x00,0x4B,0x12,0x00}));
//		list.add(getZigbeeMacString(new byte[]{(byte) 0xE7,(byte) 0xF2,0x7A,0x01,0x00,0x4B,0x12,0x00}));
//		list.add(getZigbeeMacString(new byte[]{(byte) 0xEB,(byte) 0xEF,(byte) 0xB4,0x04,0x00,0x4B,0x12,0x00}));
//		list.add(getZigbeeMacString(new byte[]{(byte) 0x8C,(byte) 0xCF,0x30,0x02,0x00,0x4B,0x12,0x00}));
//		list.add(getZigbeeMacString(new byte[]{(byte) 0xD2,(byte) 0xF1,0x7A,0x01,0x00,0x4B,0x12,0x00}));

		
		int len = 3 + list.size() * (8+2);
		int currentNum = 0;
		System.out.println("the length is --> " + len);
		byte[] create_afterpanid_network = new byte[len];
		create_afterpanid_network[0] = 0x01;
		currentNum++;
		create_afterpanid_network[1] = (byte) list.size();
		currentNum++;
		create_afterpanid_network[2] = 9;
		currentNum++;
		for(int i=0, count=list.size(); i<count; i++) {
			String mac1 = list.get(i);
			if(MacUtils.checkIfn8mac(mac1)){
				byte[] macBytes2 = MacUtils.getMacBytes(mac1);
				System.arraycopy(macBytes2, 0, create_afterpanid_network, currentNum, 8);
				currentNum+=8;
				create_afterpanid_network[currentNum++] = 0x09;
				create_afterpanid_network[currentNum++] = 0x10;
			}
		}
		System.out.println("length -> " + create_afterpanid_network.length);
		System.out.print(Arrays.toString(create_afterpanid_network));
	}

	/**
	 * zigbee的mac地址长度是8组
	 * @param mac
	 * @return
	 */
	public static byte [] getZigbeeMacBytes(String mac){
		byte []macBytes = new byte[8];
		String [] strArr = mac.split(":");

		for(int i = 0;i < strArr.length; i++){
			int value = Integer.parseInt(strArr[i],16);
			macBytes[i] = (byte) value;
		}
		return macBytes;
	}

	public static String getZigbeeMacString(byte[] macBytes) {
		String value = "";
		for(int i = 0;i < macBytes.length; i++){
			String sTemp = Integer.toHexString(0xFF &  macBytes[i]);
			value = value+sTemp+":";
		}

		value = value.substring(0,value.lastIndexOf(":"));
		System.out.println("value="+value);
		return value;
	}

}
